package com.thinkingstar.config;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

/**
 * @Author : sy
 * @Description :可以根据自己的需要定义视图拦截器
 * @Date : Created in 11:33 2018/4/17
 */

@Configuration
public class ViewConfiguration {

    private static final Logger LOGGER = LogManager.getLogger(ViewConfiguration.class);

    /**
     -	 * 配置试图模板引擎
     -	 */
    @Bean(initMethod = "init", name = "beetlConfig")
    public BeetlGroupUtilConfiguration beetlConfig() {
        LOGGER.info("[Initialize BeetlGroupUtilConfiguration]");
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        // 如果需要配置 beetl 相关功能，就传入beetl的配置文件
        // 模板的位置就在 beetl.properties 中配置
        beetlGroupUtilConfiguration.setConfigFileResource(new ClassPathResource("beetl.properties"));
        // 或简单的方式就是使用默认配置文件，用下面的代码设置模板位置
        beetlGroupUtilConfiguration.setRoot("/");
        return beetlGroupUtilConfiguration;
    }

    @Bean(name = "beetlSpringViewResolver")
    public BeetlSpringViewResolver beetlViewResolver(BeetlGroupUtilConfiguration beetlConfig) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8"); // 设置编码
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setSuffix(".html");
        beetlSpringViewResolver.setConfig(beetlConfig);
        return beetlSpringViewResolver;
    }
}
